package com.liangtian.mdm.system.datapermission.controller;

import com.liangtian.mdm.base.controller.BaseController;
import com.liangtian.mdm.base.utils.BrowserUtils;
import com.liangtian.mdm.base.utils.ResultObject;
import com.liangtian.mdm.base.utils.ShiroSessionUtils;
import com.liangtian.mdm.system.datapermission.model.SysDataPermission;
import com.liangtian.mdm.system.datapermission.service.SysDataPermissionService;
import com.liangtian.mdm.system.log.SysLogUtil;
import com.liangtian.mdm.system.log.model.SysLog;
import com.liangtian.mdm.system.log.model.SysLogEnum;
import com.liangtian.mdm.system.log.service.SysLogService;
import com.liangtian.mdm.system.user.model.SysUser;
import com.liangtian.mdm.util.ConstantBean;
import com.liangtian.mdm.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/sysDataPermissionController")
public class SysDataPermissionController extends BaseController {

    @Autowired
    private SysDataPermissionService sysDataPermissionService;

    // 日志服务接口对象
    @Autowired
    SysLogService sysLogServiceImpl;

    // Session中保存的当前登录用户的信息
    //@Value("${ConstantBean.CURRENT_SESSION_USER}")
    //private String ConstantBean.CURRENT_SESSION_USER;

    @ResponseBody
    @RequestMapping(value = "/savePermission", method = RequestMethod.POST)
    public ResultObject savePermission(HttpServletRequest request, SysDataPermission permission) {
        //获取session中用户
        SysUser user = ShiroSessionUtils.getSessionSysUser(ConstantBean.CURRENT_SESSION_USER);
        SysLog sysLog = new SysLog();
        Date date = new Date();
        sysLog.setUserName(user.getUserName());
        sysLog.setBrowserType(BrowserUtils.getBrowserName(request));
        sysLog.setOperationDate(date);
        int ret;
        try {
            ret = 1;
            String contentStr = request.getParameter("saveContent");
            if (!StringUtil.isBlank(contentStr)) {
                String userId = request.getParameter("user_id");
                if (userId != null) {
                    String[] contentArray = contentStr.split("<--->");
                    for (String test : contentArray) {
                        String[] testArray = test.split("\\|--\\|");
                        //如果前台的权限选择没有任何变化，则保留现有权限不变
                        if (testArray.length != 1) {
                            String permissionType = testArray[0];
                            String permissionArray[] = testArray[1].split(",");
                            if ("wl_dp_apply".equalsIgnoreCase(permissionType)) {
                                sysDataPermissionService.saveDataPermission(permissionArray, userId, "0");
//								sysLog.setOperationContent("用户【"+user.getUserName()+"】于"+ DateUtils.dateToString(date,"yyyy-MM-dd HH:mm:ss")+",绑定物料权限成功");
//				        		//成功记录操作日志
//				                sysLogServiceImpl.insertLog(sysLog);
                            } else {
                                sysDataPermissionService.saveDataPermission(permissionArray, userId, "1");
//								sysLog.setOperationContent("用户【"+user.getUserName()+"】于"+ DateUtils.dateToString(date,"yyyy-MM-dd HH:mm:ss")+",绑定物料权限成功");
//				        		//成功记录操作日志
//				                sysLogServiceImpl.insertLog(sysLog);
                            }
                            SysLogUtil.insertLog("新增物料权限",
                                    SysLogEnum.SYS_DATA_PERMISSION.getCode(), SysLogEnum.XZ.getCode(), request);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            ret = 0;
//			sysLog.setOperationContent("用户【"+user.getUserName()+"】于"+ DateUtils.dateToString(date,"yyyy-MM-dd HH:mm:ss")+",绑定物料权限失败");
//    		//成功记录操作日志 
//            sysLogServiceImpl.insertLog(sysLog);
        }
        int f = ret > 0 ? 1 : 0;
        ResultObject result = new ResultObject(f, "新增成功", "新增失败");
        return result;

    }

    @ResponseBody
    @RequestMapping(value = "/deleteUserWlPermission", method = RequestMethod.POST)
    public ResultObject deleteUserWlPermission(HttpServletRequest request) {
        String ids = request.getParameter("ids");
        String delFlag = request.getParameter("delFlag");
        String permissionType = "";
        if ("delWLSQ".equals(delFlag)) {//物料申请权限
            permissionType = "0";
        }
        if ("delWLSH".equals(delFlag)) {//物料审核权限
            permissionType = "1";
        }
        Map<String, Object> map = new HashMap<>();
        map.put("idList", ids.split(","));
        map.put("permission_type", permissionType);
        int ret = sysDataPermissionService.deleteUserWlPermission(map);
//		int f = ret>0? 1 : 0;
        int f = 1;
        ResultObject result = new ResultObject(f, "删除成功", "删除失败");
        return result;
    }

}
